﻿using ExamServerDAL;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace ExamServerBLL
{
    /// <summary>
    /// 配置文件类, 序列化后存储为.exam文件格式
    /// </summary>
    [Serializable]
    public class ServerConfiguration
    {
        /// <summary>
        /// socket服务端口号
        /// </summary>
        public int SocketPort { get; set; } = 8848;

        /// <summary>
        /// 状态栏是否显示时间
        /// </summary>
        public bool IsShowTime { get; set; } = true;

        /// <summary>
        /// 时间显示格式
        /// </summary>
        public string TimePattern { get; set; } = "HH:mm:ss";

        /// <summary>
        /// 是否显示ip和端口
        /// </summary>
        public bool IsShowIpAndPort { get; set; } = true;

        /// <summary>
        /// 是否显示监听的所有ip地址
        /// </summary>
        public bool IsShowAllIp { get; set; } = false;

        /// <summary>
        /// 是否显示在线人数
        /// </summary>
        public bool IsShowOnlineNum { get; set; } = true;

        /// <summary>
        /// 是否显示考试人数
        /// </summary>
        public bool IsShowExamNum { get; set; } = true;

        /// <summary>
        /// 是否开启上线提醒气泡
        /// </summary>
        public bool IsShowOnlineBubble { get; set; } = true;

        /// <summary>
        /// 数据库连接信息,仅支持mysql
        /// </summary>
        public MySQLConnectInfo SQLConnectInfo { get; set; }

        /// <summary>
        /// 读取配置文件方法
        /// </summary>
        /// <returns name="ServerConfiguration"></returns>
        public static ServerConfiguration ReadConfiguration()
        {
            ServerConfiguration configuration = null;
            try
            {
                using (FileStream fileStream = new FileStream("config.exam", FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    configuration = (ServerConfiguration)formatter.Deserialize(fileStream);
                }
                return configuration;
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: 读取配置文件出现异常: " + e.Message);
                return null;
            }
        }

        /// <summary>
        /// 写配置文件方法
        /// </summary>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static bool WriteConfiguration(ServerConfiguration configuration)
        {
            FileStream fileStream = null;
            try
            {
                // 1.创建文件流, 如果文件存在则覆盖, 不存在则创建
                fileStream = new FileStream("config.exam", FileMode.Create, FileAccess.Write);
                // 2.创建二进制格式化器
                BinaryFormatter formatter = new BinaryFormatter();
                // 3.序列化配置类并写入文件中
                formatter.Serialize(fileStream, configuration);
                // 4.关闭文件流
                fileStream.Close();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
            finally
            {
                // 最后释放资源
                if (fileStream != null)
                {
                    ((IDisposable)fileStream).Dispose();
                }
            }
        }
    }
}
